//
//  AccountSettingView.swift
//  loveHHEveryDay
//
//  Created by yw on 2025/4/12.
//

import SwiftUI
import ZLPhotoBrowser

struct AccountSettingView: View {
    @EnvironmentObject var pathManager:PathManager
    @State var list:[[String:String]] = [
        
        [
            "title":"修改名称",
            "icon":"pencil.tip.crop.circle.fill",
            "type":"push",
            "value":"",
        ],
        [
            "title":"修改头像",
            "icon":"photo.fill",
            "type":"",
            "value":"",
        ],
        [
            "title":"修改背景",
            "icon":"rectangle.fill.on.rectangle.fill",
            "type":"push",
            "value":"",
        ],
    ]
    var body: some View {
        VStack {
            ScrollView(showsIndicators: false) {
                VStack {
                    ForEach(list,id: \.self) {item in
                        if item["type"] == "push" {
                            NavigationLink(value: item["title"]) {
                                settingRow(item: item)
                            }
                        } else {
                            settingRow(item: item)
                                .contentShape(Rectangle())
                                .onTapGesture {
                                changeImageClick()
                            }
                        }
                        
                    }
                    
                }
                .padding(.vertical,10)
                .background(.white)
                .frame(width: kScreenWidth - 40)
                .cornerRadius(20)
                .padding(.top,20)
                ShakeButton(label: Text("退出登录"), textColor: .colorTextMain, fontSize: 16, fontWeight: .bold, fontDesing: .rounded) {
                    
                }.padding(.top,60)
                Text("注销账号")
                    .modifier(YWTextModifer(fontSize: 14, fontWeight: .light, design: .rounded, color: .colorTextSub))
                    .shakeTapGes {
                    
                    }
            }
            
        }
        .navigationTitle("账户管理")
        .navigationBarTitleDisplayMode(.inline)
        .frame(maxWidth: .infinity)
        .background(Color("color_bg"))
        .environmentObject(pathManager)
        .navigationBack()
        
    }
    
    private func settingRow(item:[String:String]) -> some View {
        HStack {
            Image(systemName: item["icon"]!)
                .resizable()
                .frame(width: 16, height: 16)
                .aspectRatio(contentMode: .fill)
                .foregroundColor(.colorMain)
            
            Text(item["title"]!)
                .modifier(YWTextModifer(fontSize: 14, fontWeight: .medium, design: .rounded, color: Color("color_text_main")))
                .padding(.leading,6)
            Spacer()
            Image(systemName: "arrow.right")
                .resizable()
                .frame(width: 12, height: 12)
                .aspectRatio(contentMode: .fill)
                .foregroundColor(Color("color_text_sub"))
        }.padding(.horizontal,16)
            .padding(.vertical,16)
    }
    
    private func changeImageClick() {
        let view = AlertRowsView(frame: CGRectMake(0, 0, kScreenWidth, 200)) { tag in
            DPrint(tag)
            if tag == 1 {
                showCamera()
            } else if tag == 0 {
                selectPhoto()
            }
        }
        view.show(.sheet, backgoundTapDismissEnable: true)
    }
    
    private func showCamera() {
        let scene = UIApplication.shared.connectedScenes.first
        let root = (scene as? UIWindowScene)?.windows.first?.rootViewController
        if root != nil {
//            let vc = (root as? UINavigationController)?.visibleViewController
            ZLPhotoConfiguration.default().allowEditVideo = false
            ZLPhotoConfiguration.default().allowSelectVideo = false
            ZLPhotoConfiguration.default().allowSelectVideo = false
            ZLPhotoConfiguration.default().allowSelectLivePhoto = false
            let camera = ZLCustomCamera()
            camera.takeDoneBlock = { image,url in
                DPrint(image)
//                DPrint(url)
            }

            root?.present(camera, animated: true)
        }
    }
    
    private func selectPhoto() {
        let scene = UIApplication.shared.connectedScenes.first
        let root = (scene as? UIWindowScene)?.windows.first?.rootViewController
        if root != nil {
//            let vc = (root as? UINavigationController)?.visibleViewController
            ZLPhotoConfiguration.default().maxSelectCount = 1
            ZLPhotoConfiguration.default().allowEditVideo = false
            ZLPhotoConfiguration.default().allowSelectVideo = false
            ZLPhotoConfiguration.default().allowSelectVideo = false
            ZLPhotoConfiguration.default().allowSelectLivePhoto = false
            let ac = ZLPhotoPreviewSheet()
            ac.selectImageBlock = {  results,isOriginal in
//                let img = results.first?.image
//                guard let imageStr = img?.base64 else {
//                    YWProgressHUD.showTextHudTips(message: NSLocalizedString("t2v_title_18", comment: ""))
//                    return
//                }
//                self.selectImageBase64 = imageStr
//                self.selectImage = img ?? UIImage()
            }
            ac.showPhotoLibrary(sender: root!)
        }
        
    }
    
}

